package gx.web.controller;

import java.util.List;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import gx.entity.Category;
import gx.entity.Product;
import gx.entity.Purchase;
import gx.entity.Store;
import gx.entity.common.Page;
import gx.service.CategoryService;
import gx.service.ProductService;
import gx.service.PurchaseService;
import gx.service.StoreService;

@Controller
@RequestMapping("/mng/store")
public class StoreController {
	@Autowired
	private PurchaseService purchaseService;

	@Autowired
	private ProductService productService;
	
//	@Autowired 
//	private StoreService storeService;
	@Autowired 
	private CategoryService categoryService;
	
	@RequestMapping("/list.jhtml" )
	public ModelAndView list(@RequestParam(name = "size", defaultValue = "10") int size,
			@RequestParam(name = "number", defaultValue = "1") int number) throws Exception {
		ModelAndView mav = new ModelAndView("/mng/store/list");
	//	Page<Store> page= storeService.findByPager(size, number);
		Page<Product> page= productService.findByPager(size, number);
 		mav.addObject("page", page);
 	
		return mav;
	}
	// 动态查询
			@RequestMapping("/find.jhtml")
			public ModelAndView find(@RequestParam(name = "size", defaultValue = "10") int size,
					@RequestParam(name = "number", defaultValue = "1") int number, Integer status, String startDate,
					String endDate) throws ParseException {
				// 离线查询条件对象 "from Purchase"
				DetachedCriteria dc = DetachedCriteria.forClass(Purchase.class);
				DetachedCriteria dc2 = DetachedCriteria.forClass(Purchase.class);
				DetachedCriteria dc3 = DetachedCriteria.forClass(Purchase.class);

				// "from Purchase where status?"
				if (status != null && status.intValue() >= 0) {
					dc.add(Property.forName("status").eq(status)); // eq: 等于
					dc2.add(Property.forName("status").eq(status));
					dc3.add(Property.forName("status").eq(status));
				} else {
					Criterion c = Restrictions.or(Restrictions.eq("status", 1), Restrictions.eq("status", 2));
					dc.add(c);
					dc2.add(c);
					dc3.add(c);
				}
				DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

				// "from Purchase where status=? and pur_date>? "
				if (null != startDate && !"".equals(startDate)) {
					dc.add(Property.forName("pur_date").ge(df.parse(startDate)));// ge:大于等于
					dc2.add(Property.forName("pur_date").ge(df.parse(startDate)));
					dc3.add(Property.forName("pur_date").ge(df.parse(startDate)));
				}

				// "from Purchase where status=? and pur_date>? and pur_date<?"
				if (null != endDate && !"".equals(endDate)) {
					dc.add(Property.forName("pur_date").le(df.parse(endDate)));// le:
																				// 小于等于
					dc2.add(Property.forName("pur_date").ge(df.parse(startDate)));
					dc3.add(Property.forName("pur_date").ge(df.parse(startDate)));
				}

				dc.addOrder(Order.desc("id"));
				dc2.setProjection(Projections.count("id"));
				dc3.setProjection(Projections.count("id"));

				Page<Purchase> page = purchaseService.findByCriteria(size, number, dc, dc2, dc3);

				ModelAndView mav = new ModelAndView("/mng/stor/list");
				mav.addObject("page", page);

				return mav;
			}
			@RequestMapping("/detail.jhtml")
			public ModelAndView detail(Integer id){
				ModelAndView mav=new ModelAndView("/mng/store/detail");
				Product product=productService.get(id);
				mav.addObject("prod",product);
				Category cate=product.getCategory();
				mav.addObject("cate",cate);
				return mav;
			}
			//根据Id修改
			@RequestMapping("/edit.jhtml")
			public String edit(Integer id,Integer num,Integer security_num){
				Product product=productService.get(id);
				product.setNum(num);
				product.setSecurity_num(security_num);
				productService.update(product);
				return "redirect:/mng/store/list.jhtml";

			}
	
	//商品转仓
//	@RequestMapping("/list.jhtml")
////	public ModelAndView list(@RequestParam(name="size",defaultValue="10")int size,
////	@RequestParam(name="number",defaultValue="1")int number){
////		
////	ModelAndView mav=new ModelAndView("/mng/store/list");
////	Page<Store> page=storeService.findByPager(size,number);
////	mav.addObject("page",page);
////	return mav;
////	}
//	
}
	
	
	


